﻿/*=========================================================================
 * AUTH: Vincent Breton
 * DESC: sub-class for drawing Menu Screens
 * REF: Stellar Siege Project 2010
 *=========================================================================*/
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace RisingThreat
{

    class MenuScreen:Screen
    {
        //set up the title
        public SpriteFont titleFont;
        public Vector2 titlePos;
        public String title = "RISING THREAT";
        public Texture2D background;
        public SpriteFont menuFont;
        public Vector2 menuPos;
        public String[] options;
        int currentOption;
        const float LINE_SPACING = 20f;
        public Boolean firstTimeInMenu = true;
        GraphicsDeviceManager graphics;
        public double time=0;


        public MenuScreen(ContentManager content, EventHandler gameEvent, GraphicsDeviceManager graphs)
            : base(gameEvent)
        {
            graphics = graphs;
            String prefix = (graphics.PreferredBackBufferHeight >= 600) ? "" : "low-res_";

            titleFont = content.Load<SpriteFont>("fonts/Cutscene");
            menuFont = content.Load<SpriteFont>("fonts/Cutscene");

         if (graphics.PreferredBackBufferHeight < 600)
          {
                titlePos = new Vector2(graphics.GraphicsDevice.Viewport.TitleSafeArea.X + 40,
                    graphics.GraphicsDevice.Viewport.TitleSafeArea.Y + 50);
                menuPos = new Vector2(titlePos.X + 75, titlePos.Y + 30 + titleFont.MeasureString(title).Y);
         }
          else
           {

              titlePos = new Vector2(40.0f, 60.0f);
            menuPos = new Vector2(100.0f, 200.0f);
          }

            options = new String[] {};
            currentOption = 0;
            background = content.Load<Texture2D>("textures/fire");

        }

        public void setFirstTimeInMenu(Boolean bo)
        {
            firstTimeInMenu=bo;
        }

        public void setOptions(String[] listOptions)
        {
            options = listOptions;
        }

        /*public void setTime(double t)
        {
            time = t;
        }*/

        public override void Update(GameTime gametime)
        {
            KeyboardState keyboardState = Keyboard.GetState();
            GamePadState var = GamePad.GetState(PlayerIndex.One);

                if (firstTimeInMenu && !(keyboardState.IsKeyDown(Keys.Enter) || var.Buttons.A == ButtonState.Pressed) )
                {
                    firstTimeInMenu = false;
                }
                else if (!firstTimeInMenu && (keyboardState.IsKeyDown(Keys.Enter) || (var.Buttons.A == ButtonState.Pressed )))
                {
                    screenEvent.Invoke(this, new ScreenArgs(currentOption));
                }

                //Menu Selection

                if (var.DPad.Up == ButtonState.Pressed || keyboardState.IsKeyDown(Keys.Up))
                {
                    if (time == 0)
                    {
                        time = gametime.TotalGameTime.TotalSeconds;
                        if (currentOption > 0)
                        {
                            currentOption -= 1;
                        }
                        else if (currentOption == 0)
                        {
                            currentOption = (options.Length - 1);
                        }
                    }
                    else if ((time + 0.2) < gametime.TotalGameTime.TotalSeconds)
                    {
                        time = 0;
                    }
                }
                if (var.DPad.Down == ButtonState.Pressed || keyboardState.IsKeyDown(Keys.Down))
                {
                    if (time == 0)
                    {
                        time = gametime.TotalGameTime.TotalSeconds;
                        if (currentOption < (options.Length - 1))
                        {
                            currentOption += 1;
                        }
                        else if (currentOption == (options.Length - 1))
                        {
                            currentOption = 0;
                        }
                    }
                    else if ((time + 0.2) < gametime.TotalGameTime.TotalSeconds)
                    {
                        time = 0;

                    }

                }

            

/*            if (var.DPad.Up == ButtonState.Released && keyboardState.IsKeyUp(Keys.Up))
            {
                if (currentOption > 0)
                {
                    upPressed = false;
                }
            }
            if (var.DPad.Down == ButtonState.Released && keyboardState.IsKeyUp(Keys.Down))
            {
                if (currentOption < (options.Length - 1))
                {
                    downPressed = false;
                }
            }*/
          
            
            base.Update(gametime);

        }

        public override void Draw(SpriteBatch batch)
        {
            Vector2 currentPosition = menuPos;

            //draw background
            batch.Draw(background, new Rectangle(0, 0, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight), null, Color.White);
            //Draw Title
            batch.DrawString(titleFont, title, titlePos, Color.White);
            //Draw Menu


            for (int i = 0; i < options.Length; i++)
            {
                if (i == currentOption)
                {
                    batch.DrawString(menuFont, options[i], currentPosition, Color.Yellow);
                }
                else
                {
                    batch.DrawString(menuFont, options[i], currentPosition, Color.White);
                }

                currentPosition.Y += menuFont.MeasureString(options[i]).Y + LINE_SPACING;
            }

           
            base.Draw(batch);

        }
    }
}
